//
//  UserEarningsController.m
//  CheekiOS
//
//  Created by mac on 2017/6/21.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "UserEarningsController.h"
//提现
#import "UserWithdrawalController.h"
//提现记录
#import "UserEarningRecordController.h"
//兑换钻石
#import "UserExchangeDiamondController.h"
@interface UserEarningsController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong)UserpersionlInfoModel *model;

/**
 兑换比例
 */
@property (nonatomic, strong)NSString *feeStr;

/**
 判断是否隐藏
 */
@property (nonatomic, strong)NSNumber *UserTypehidden;

@end

@implementation UserEarningsController

-(void)viewWillAppear:(BOOL)animated{
    [self setNavgationBarHidden];
    [self requestUserInfo];
    [self requestFee];
    
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 64)];
    bgView.backgroundColor = Colorbackground;
    [self.view addSubview:bgView];
    
    
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(22, 19.5, 44, 44)];
    btn.contentHorizontalAlignment=UIControlContentHorizontalAlignmentLeft ;//设置文字位置，现设为居左，默认的是居中
    
    [btn setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    btn.imageEdgeInsets = UIEdgeInsetsMake(0, 5.5, 0, 0);
    [btn addTarget:self action:@selector(backActionbtn) forControlEvents:UIControlEventTouchUpInside];
    
    CGFloat TopsegmentFloat ;
    
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad){
        NSLog(@"ipad");
        TopsegmentFloat =0;
        
    }else{
        TopsegmentFloat = 16;
        
        NSLog(@"iphone or ipod");
    }
    
    UILabel *titleLab = [[UILabel alloc] initWithFrame:CGRectMake(0, TopsegmentFloat, KScreenWidth, 44*SCREEN_WIDTH_RATIO47)];
    titleLab.text = @"收益";
    titleLab.font = [UIFont systemFontOfSize:18];
    titleLab.textColor = ColorI(0x2e353f);
    titleLab.textAlignment = NSTextAlignmentCenter;
    [bgView addSubview:titleLab];
    [bgView addSubview:btn];
    
    UIImageView *headerView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 205*SCREEN_WIDTH_RATIO47)];
    headerView.image = [UIImage imageNamed:@"grzx_profit_illustration"];
    headerView.contentMode = UIViewContentModeScaleAspectFill;
    headerView.userInteractionEnabled = YES;

    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightBtn addTarget:self action:@selector(rightBtndetails) forControlEvents:UIControlEventTouchUpInside];
    [rightBtn setTitle:@"收益明细" forState:UIControlStateNormal];
    rightBtn.frame = CGRectMake(headerView.width-70-15, headerView.bottom-55*SCREEN_WIDTH_RATIO47, 70, 25);
    rightBtn.titleLabel.font = FontP(15);
    [rightBtn setTitleColor:ColorI(0x2d353f) forState:UIControlStateNormal];
    
    [headerView addSubview:rightBtn];
    
    
    if (!_tableView) {
        self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, KScreenWidth, KScreenHeight-49) style:UITableViewStylePlain];
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        self.tableView.backgroundColor = [UIColor whiteColor];
        self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self.view addSubview:_tableView];
    }
    
    _tableView.tableHeaderView = headerView;
    [self requestgetCommonOpen];
}
- (void)requestgetCommonOpen{
    [HcardHttpmanager getCommonOpen:[HQHttpManager getUserId] success:^(id result) {
        if([result[@"code"] isEqualToString:@"ok"]){
            _UserTypehidden = result[@"res"][@"open"];
            

        }
        [_tableView reloadData];    
    } failure:^(NSError *error) {
        
    }];
    
}

- (void)backActionbtn{
    self.navigationController.navigationBar.hidden = NO;
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)rightBtndetails{
    //收益明细

    UserEarningRecordController *vc = [[UserEarningRecordController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];

}
- (void)requestUserInfo{
    
    [HcardHttpmanager getPersionalInfosuccess:^(id sucess) {
        
        self.model = [UserpersionlInfoModel mj_objectWithKeyValues:sucess[@"res"]];

        
        [self.tableView reloadData];
        
    } failure:^(NSError *error) {
        
    }];
}

- (void)requestFee{
    
    [HcardHttpmanager getFeecharm1ToCostSuccess:^(id result) {
        
        _feeStr =result;
        
    } failure:^(NSError *error) {
        
    }];
}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 3;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        static NSString *UserEarningCellIdentOne = @"UserEarningCellIdentOne";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:UserEarningCellIdentOne];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:UserEarningCellIdentOne];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        for (UIView * subView in cell.contentView.subviews){
            if ([subView isKindOfClass:[UILabel class]]) {
                [subView removeFromSuperview];
            }
        }
        
        
        UILabel *numberLab = [UILabel new];
        numberLab.textAlignment = NSTextAlignmentCenter;
        numberLab.textColor = ColorI(0x5978b6);
        numberLab.font = FontP(25);
        numberLab.text = [NSString stringWithFormat:@"%.2f",[_model.charm1 floatValue]];
        numberLab.frame = CGRectMake(0, 5, KScreenWidth, 40);
        
        UILabel *diamondLab = [UILabel new];
        
        diamondLab.textAlignment = NSTextAlignmentCenter;
        diamondLab.textColor = [UIColor blackColor];
        diamondLab.font = FontP(16);
        diamondLab.text = @"魅力值";
        diamondLab.frame = CGRectMake(0, numberLab.bottom+15, KScreenWidth, 25);
        
        UILabel *haveLab = [UILabel new];
        haveLab.frame = CGRectMake(self.view.centerX-65, diamondLab.bottom+12, 130, 30);
        haveLab.text =  @"拥有魅力值";
        haveLab.textColor = Colorbackground;
        haveLab.layer.cornerRadius=15;

        haveLab.textAlignment = NSTextAlignmentCenter;
        [haveLab.layer setMasksToBounds:YES];
        [haveLab.layer setBorderWidth:1.0]; //边框宽度
        haveLab.layer.borderColor=Colorbackground.CGColor;
        
        
        UILabel *linlab = [UILabel new];
        linlab.backgroundColor = RGB(243, 243, 243);
        linlab.frame = CGRectMake(50, haveLab.bottom+35, KScreenWidth-100, 1);
        
        [cell.contentView addSubview:numberLab];
        [cell.contentView addSubview:diamondLab];
        [cell.contentView addSubview:haveLab];
        [cell.contentView addSubview:linlab];
        
        return cell;
    }else if (indexPath.row == 1){
        static NSString *UserEarningCellIdent = @"UserEarningCellIdent";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:UserEarningCellIdent];
        
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:UserEarningCellIdent];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        for (UIView * subView in cell.contentView.subviews){
            if ([subView isKindOfClass:[UILabel class]]) {
                [subView removeFromSuperview];
            }
        }
        
        
        UILabel *numberLab = [UILabel new];
        numberLab.textAlignment = NSTextAlignmentCenter;
        numberLab.textColor = Colorbackground;
        numberLab.font = FontP(25);
        
        
        
        numberLab.text = [NSString stringWithFormat:@"%.2f",([_model.charm1 floatValue]*[_feeStr floatValue])];
        
        numberLab.frame = CGRectMake(0, 5, KScreenWidth, 40);
        
        UILabel *diamondLab = [UILabel new];
        diamondLab.textAlignment = NSTextAlignmentCenter;
        diamondLab.textColor = [UIColor blackColor];
        diamondLab.font = FontP(16);
        diamondLab.text = @"可提现金额";
        diamondLab.frame = CGRectMake(0, numberLab.bottom, KScreenWidth, 25);

        [cell.contentView addSubview:numberLab];
        [cell.contentView addSubview:diamondLab];
        
        
        UIButton *diamondbtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [diamondbtn addTarget:self action:@selector(diamondAction) forControlEvents:UIControlEventTouchUpInside];
        [diamondbtn setTitle:@"兑换钻石" forState:UIControlStateNormal];
        [diamondbtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        diamondbtn.hidden = YES;

        diamondbtn.frame = CGRectMake(25, diamondLab.bottom+47, KScreenWidth/2-25-12.5, 50);
        diamondbtn.backgroundColor = [UIColor whiteColor];
        diamondbtn.layer.cornerRadius=25;
        [diamondbtn.layer setBorderWidth:1.0];
        diamondbtn.layer.borderColor=Colorbackground.CGColor;

        if ([_UserTypehidden isEqualToNumber:@1]) {
            diamondbtn.hidden = NO;
            diamondLab.hidden = NO;
            numberLab.hidden = NO;
        }else{
            diamondbtn.hidden = YES;
            diamondLab.hidden = YES;
            numberLab.hidden = YES;
        }
        
        [cell.contentView addSubview:diamondbtn];
        
        
        UIButton *btnImg = [UIButton buttonWithType:UIButtonTypeCustom];
        [btnImg addTarget:self action:@selector(withdrawalAction) forControlEvents:UIControlEventTouchUpInside];
        [btnImg setTitle:@"提现" forState:UIControlStateNormal];
        [btnImg setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        btnImg.hidden = YES;
        btnImg.frame = CGRectMake(diamondbtn.right+25, diamondLab.bottom+47, KScreenWidth/2-25-12.5, 50);
        btnImg.backgroundColor = Colorbackground;
        btnImg.layer.cornerRadius=25;
        if ([_UserTypehidden isEqualToNumber:@1]) {
            btnImg.hidden = NO;
        }else{
            btnImg.hidden = YES;
            
        }
        
        [cell.contentView addSubview:btnImg];
        
        return cell;
    }else{
    
        static NSString *UserEarningCellIdentOne = @"UserEarningCellIdentOne";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:UserEarningCellIdentOne];
        
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:UserEarningCellIdentOne];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        for (UIView * subView in cell.contentView.subviews){
            if ([subView isKindOfClass:[UILabel class]]) {
                [subView removeFromSuperview];
            }
        }
        
        
        UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(0, 5,KScreenWidth , 20)];
        
        if ([_UserTypehidden isEqualToNumber:@1]) {
            lab.hidden = YES;
            lab.text = @"收到礼物后钻石会自动兑换成魅力值，魅力值可购买钻石，也可提现";
        }else{
            lab.hidden = NO;
//            lab.text = @"收到礼物后钻石会自动兑换成魅力值，魅力值可购买钻石";
            
        }
        lab.textColor = ColorI(0x2e353f);
        lab.textAlignment = NSTextAlignmentCenter;
        lab.font = FontP(10);
        [cell addSubview:lab];
        return cell;
    }
    return nil;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 2) {
        return 45;
    }else{
        return 175;

    }
}

- (void)withdrawalAction{
    
    UserWithdrawalController *vc = [[UserWithdrawalController alloc]init];
    
    [self.navigationController pushViewController:vc animated:YES];
    NSLog(@"提现");
 

}

- (void)diamondAction{

    UserExchangeDiamondController *vc = [[UserExchangeDiamondController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
    NSLog(@"兑换钻石");
}



@end
